package com.example.service.impl;

import cn.hutool.core.io.FileUtil;
import com.example.context.BaseContext;
import com.example.entity.Script;
import com.example.entity.Storyboardfiles;
import com.example.mapper.StoryboardfilesMapper;
import com.example.service.IStoryboardfilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.utils.BaseException;
import com.example.utils.CommonControllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈昌吉
 * @since 2024-04-16
 */
@Service
public class StoryboardfilesServiceImpl extends ServiceImpl<StoryboardfilesMapper, Storyboardfiles> implements IStoryboardfilesService {


    @Autowired
    private CommonControllers commonControllers;

    @Autowired
    private StoryboardfilesMapper storyboardfilesMapper;

    @Override
    public List<Storyboardfiles> insert(MultipartFile file, Integer hid) {
        String originalFilename = file.getOriginalFilename();  //获取文件原始名称
        String s = FileUtil.mainName(originalFilename);  //获取文件前面名不含后缀
        String s1 = FileUtil.extName(originalFilename);  //获取文件后缀名字

        String upload = String.valueOf(commonControllers.upload(file));  //获取返回时的url

        Storyboardfiles build = Storyboardfiles.builder()
                .storyboardFile(upload)
                .loginId(BaseContext.getCurrentId())
                .name(originalFilename)
                .uploadtime(LocalDateTime.now())
                .hid(hid)
                .build();

        Integer currentId = BaseContext.getCurrentId();
        List<Storyboardfiles> storyboardfiles = storyboardfilesMapper.selectStory(currentId,hid);
        for (int i=0;i<storyboardfiles.size();i++){
            if (storyboardfiles.get(i).getName().equals(originalFilename)){
                throw new BaseException("该文件已存在，不能重复上传",500);
            }
        }
        int insert = storyboardfilesMapper.insert(build);
        List<Storyboardfiles> storyboardfiles1 = storyboardfilesMapper.selectStory(currentId, hid);
        return storyboardfiles1;
    }

    @Override
    public List<Storyboardfiles> getAll(Integer hid) {
        Storyboardfiles a = new Storyboardfiles();
        a.setLoginId(BaseContext.getCurrentId());
        a.setHid(hid);
        return storyboardfilesMapper.selectStorys(a);
    }

    @Transactional
    public String deleteFile(Storyboardfiles storyboardfiles) {
        String delete = commonControllers.delete(storyboardfiles.getStoryboardFile());
        storyboardfilesMapper.deleteStory(storyboardfiles);
        return delete;
    }

    /**
     * 审核员处获取所有的作业
     *
     * @param storyboardfiles
     * @return
     */
    @Override
    public List<Storyboardfiles> getStory(Storyboardfiles storyboardfiles) {
        List<Storyboardfiles> story= storyboardfilesMapper.selectStory1(storyboardfiles);
        return story;
    }

    /**
     * 获取作业状态
     * @param hids
     * @return
     */
    @Override
    public LinkedHashSet<Storyboardfiles> getStoryState(ArrayList<Integer> hids) {
        Storyboardfiles storyboardfiles = new Storyboardfiles();
        Integer currentId = BaseContext.getCurrentId();
        List<Storyboardfiles> state= storyboardfilesMapper.getState(currentId,hids);
        LinkedHashSet<Storyboardfiles> hashSet = new LinkedHashSet<>(state);
        return hashSet;
    }

    /**
     * 审核对提交的作业进行审核
     * @param storyboardfiles
     * @return
     */
    @Override
    public void updateState(Storyboardfiles storyboardfiles) {
        System.out.println(storyboardfiles);
        Integer i = storyboardfilesMapper.updateState(storyboardfiles);
    }

}
